//
//  UrbanLogInTextFild.m
//  UrbanManagement
//
//  Created by Can on 18/4/8.
//  Copyright © 2018年 hwd. All rights reserved.
//

#import "UrbanLogInTextFild.h"

@interface UrbanLogInTextFild()<UITextFieldDelegate>
{
    NSInteger _maxNum;
}

@property (nonatomic,strong)NSTimer *timer;
@property (nonatomic,assign)int s;
@end

@implementation UrbanLogInTextFild

- (instancetype)initWithFrame:(CGRect)frame imageName:(NSString *)imageName placeholder:(NSString *)placeholder keyboardType:(UIKeyboardType)keyboardType maxNum:(NSInteger)maxNum type:(UrbanLogInTextFildType)type
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake(frame.origin.x, frame.origin.y, SCREENW - 60*screenW_6, 50*screenW_6);
        
        [self creatViewWithImageName:imageName placeholder:placeholder keyboardType:keyboardType type:type];
        if (maxNum > 0) {
            _maxNum = maxNum;
        }
    }
    return self;
}

-(void)creatViewWithImageName:(NSString *)imageName placeholder:(NSString *)placeholder keyboardType:(UIKeyboardType)keyboardType type:(UrbanLogInTextFildType)type
{
    //左侧图片
    UIImageView *imageV = [TomamaUI createImageView:CGRectMake(12.5*screenW_6, 0, 14*screenW_6, 17*screenW_6) backGroundImageV:imageName];
    [self addSubview:imageV];
    imageV.center = CGPointMake(imageV.center.x, self.frame.size.height/2);
    
    //右侧按钮
    CGFloat length = 0;
    if (type == UrbanLogInTextFildPassWordType) {
        UIButton *btn = [TomamaUI createButton:0 Frame:CGRectMake(self.frame.size.width - 40*screenW_6, 0,40*screenW_6,40*screenW_6) targ:self sel:@selector(hiddenText:) titleColor:nil font:nil title:nil image:@"eye_off" backGroundImage:nil];
        [btn setImage:[UIImage imageNamed:@"eye_on"] forState:UIControlStateSelected];
        [self addSubview:btn];
        btn.center =CGPointMake(btn.center.x, imageV.center.y);
        length = 22*screenW_6;
        //18 ＊ 12
        [btn setImageEdgeInsets:UIEdgeInsetsMake(14*screenW_6,18*screenW_6, 14*screenW_6,4*screenW_6)];
    }else if (type == UrbanLogInTextFildMsgType){
        _btn = [TomamaUI createButton:0 Frame:CGRectMake(self.frame.size.width - 65*screenW_6, 0, 60*screenW_6, 15*screenW_6) targ:self sel:@selector(sendMsg:) titleColor:Color(21, 121, 242) font:[UIFont systemFontOfSize:15*screenW_6] title:@"发送" image:nil backGroundImage:nil];
        [self addSubview:_btn];
        _btn.center =CGPointMake(_btn.center.x, imageV.center.y);
        length = 22*screenW_6;
    }
    
    //输入
    UITextField *textF = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imageV.frame) + 20*screenW_6, 0, self.frame.size.width - CGRectGetMaxX(imageV.frame) - 35*screenW_6 - length,40*screenW_6)];
    textF.center =CGPointMake(textF.center.x, imageV.center.y);
    textF.font = [UIFont systemFontOfSize:15*screenW_6];
    textF.textColor = Color(153, 153, 153);
    textF.placeholder = placeholder;
    if (type == UrbanLogInTextFildPassWordType) {
        textF.secureTextEntry = YES;
    }
    
    [IQKeyboardManager sharedManager].keyboardDistanceFromTextField = 100*screenW_6;
    
    [textF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    if (keyboardType) {
        textF.keyboardType = keyboardType;
    }
    
    textF.returnKeyType = UIReturnKeyDone;
    textF.tag = 100;
    textF.delegate = self;
    [self addSubview:textF];
    
//    UIToolbar *bar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0, SCREENW,44)];
//    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(SCREENW - 60, 7,50, 30)];
//    [button setTitle:@"完成"forState:UIControlStateNormal];
//    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(printDone)forControlEvents:UIControlEventTouchUpInside];
//    [bar addSubview:button];
//    textF.inputAccessoryView = bar;
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 1, self.frame.size.width, 1)];
    lineView.backgroundColor = Color(204, 204, 204);
    [self addSubview:lineView];
}

-(void)textFieldDidChange:(UITextField *)textField
{
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    if (!position && _maxNum) {
        if (textField.text.length > _maxNum) {
            textField.text = [textField.text substringToIndex:_maxNum];
        }
    }
    if (_UrbanLogInTextFildblock) {
        _UrbanLogInTextFildblock(textField.text);
    }
}

-(void)hiddenText:(UIButton *)btn
{
    btn.selected = !btn.selected;
    UITextField *textF = [self viewWithTag:100];
    textF.secureTextEntry = !btn.selected;
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)aTextfield {
    [aTextfield resignFirstResponder];//关闭键盘
    return YES;
}

-(void)sendMsg:(UIButton *)btn
{
    
}

//- (void)printDone
//{
//    UITextField *textF = [self viewWithTag:100];
//    [textF resignFirstResponder];//关闭键盘
//}

@end
